package com.tange.ai.core.demo.add.scan;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.tange.ai.core.demo.Utils;
import com.tange.ai.core.demo.add.ap.WiFiConfigurationActivity;
import com.tange.ai.core.demo.databinding.ActivityScanWifiConfigurationBinding;
import com.tange.base.toolkit.PreferenceUtil;

/**
 * WiFi配置并生成二维码，然后向设备展示
 */
public class ScanWiFiConfigurationActivity extends AppCompatActivity {

    private ActivityScanWifiConfigurationBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityScanWifiConfigurationBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        getSupportActionBar().hide();

        Utils.setStatusBar(this, Color.TRANSPARENT);


        String preWifiName = PreferenceUtil.getString(this, WiFiConfigurationActivity.WIFI_NAME, "");
        String preWifiPwd = PreferenceUtil.getString(this, WiFiConfigurationActivity.WIFI_PASSWORD, "");

        binding.wifiName.setText(preWifiName);
        binding.wifiPwd.setText(preWifiPwd);

        binding.generateQrCode.setOnClickListener(view -> {

            String wifiName = binding.wifiName.getText().toString();
            String wifiPwd = binding.wifiPwd.getText().toString();

            if (TextUtils.isEmpty(wifiName)) {
                Toast.makeText(view.getContext(), "填写WIFI信息！", Toast.LENGTH_SHORT).show();
                return;
            }

            PreferenceUtil.setString(this, WiFiConfigurationActivity.WIFI_NAME, wifiName);
            PreferenceUtil.setString(this, WiFiConfigurationActivity.WIFI_PASSWORD, wifiPwd);

            Intent intent = new Intent(view.getContext(), QrCodeShowActivity.class);
            intent.putExtra(WiFiConfigurationActivity.WIFI_NAME, wifiName);
            intent.putExtra(WiFiConfigurationActivity.WIFI_PASSWORD, wifiPwd);
            startActivity(intent);

        });
    }
}